package com.zhxy.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.EquipmentRepairDao;
import com.zhxy.manager.dto.EquipmentRepairDto;
import com.zhxy.manager.entity.EquipmentRepair;
import com.zhxy.manager.entity.LibrarySeat;
import com.zhxy.manager.service.intf.EquipmentRepairService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class EquipmentRepairServiceImpl implements EquipmentRepairService{
    @Autowired
    private EquipmentRepairDao dao;


    @Override
    public R save(EquipmentRepair equipmentRepair) {
        if (dao.add(equipmentRepair)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public List<EquipmentRepair> queryPage() {
        return dao.selectAll();
    }

    @Override
    public R del(int id) {
        if (id>0){
            if (dao.del(id)>0){
                return R.ok();
            }else{
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }

    @Override
    public R update(EquipmentRepair equipmentRepair) {
        if (dao.update(equipmentRepair)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public PageBean queryPageWhere(String name, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(name)){
            name=null;
        }else{
            name="%"+name+"%";//包含  模糊查询
        }
        PageHelper.startPage(page,limit);
        PageInfo<EquipmentRepair> pageInfo = new PageInfo<>(dao.selectWhere(name));
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        if (ids != null & ids.length > 0) {
            if (dao.delBatch(ids) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }

    @Override
    public PageBean queryDetail(String name, int page, int limit) {
        //1.条件处理
        if(StringUtils.hasLength(name)){
            name="%"+name+"%";
        }else{
            name=null;
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<EquipmentRepairDto> pageInfo=new PageInfo<>(dao.selectDetail(name));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }
}
